import requests

class GithubStars:
    """github上星标排名"""

    def __init__(self, language) -> None:
        self.language = language

    def stars_ranking(self) -> dict:
        """解析获取星标最高排名"""

        param = f"q=language:{self.language}&sort=stars"
        url = f"https://api.github.com/search/repositories?{param}"

        heads = {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}

        # 请求接口
        r = requests.get(url=url, headers= heads)

        print(f"请求状态:{r.status_code}")

        # 转换为 json
        r_dict = r.json()

        print(f"总共项目:{r_dict["total_count"]}")
        print(f"公开项目:{len(r_dict["items"])}")
        # 拿取项目明细

        r_items = r_dict["items"]
        # print(r_items[0].keys())
        names, stars, descriptions = [],[],[]
        # 拿取项目明细信息
        for item in r_items:
            # print("=========================")
            # print(f"项目名称:{item['name']}")
            # print(f"创建人:{item['owner']['login']}")
            # print(f"仓库地址:{item['html_url']}")
            # print(f"星标:{item['stargazers_count']}")
            # print(f"创建时间:{item['created_at']}")
            # print(f"修改时间:{item['updated_at']}")
            # print(f"说明:{item['description']}")
            repo_url = item['html_url']
            names.append(f"<a href='{repo_url}'>{item['name']}</a>")
            stars.append(item['stargazers_count'])
            descriptions.append(f"{item['owner']['login']}<br>{item['description']}")
        
        return {'name': names, 'stars': stars, 'description': descriptions}